<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 任务
 */
class Task extends Api
{

    
    protected $noNeedLogin = ['*'];
 
    protected $noNeedRight = ['*'];


    /**
     * 获取图片
     *
     * @param integer $limit
     * @return void
     */
    public function get_image($limit=2000)
    {
        $ci = 0;
        $list = Db::name('door_log')->where('image',null)->where('tkid','>',0)->limit($limit)->select();
        // halt($list);
        if($list){
            foreach ($list as $key => $val) {
                $url =  'http://106.52.152.213/pictures/'.$val['tkid'].'.jpg';
                
                try {
                    $content = file_get_contents($url);
                    if(strlen($content)>10){
                        
                        $img_path = 'pic/'.$val['tkid'].'.jpg';
                        // halt($img_path);
                        file_put_contents($img_path,$content);
                        // halt($this->request->domain().'/'.$img_path);
                        
                        $res = Db::name('door_log')->where('id',$val['id'])->update(['image'=>'/'.$img_path]);
                        if($res)$ci++;
                    }
                } catch (\Throwable $th) {
                    //throw $th;
                }
            }       
            echo $ci;
        }else{
            $list = Db::name('door_log')->where('image',null)->where('tkid',null)->select();
            if($list){
                $card=$this->card_db();
                foreach ($list as $key => $val) {
    
                    
                    $where['cardnum'] = $val['card'];
                    // $where['gatenum']  = $val['gate'];
                    // $where['entrance']  = $val['entrance'];
                    $where['accessstat'] = $val['accessstat'];
                    $where['CreatedTime']  = date('Y-m-d H:i:s',$val['time']);
                    
                    $ids = $card->name('accessrecord')->where($where)->column('tkid');
                    if(sizeof($ids)==1){
                        Db::name('door_log')->where('id',$val['id'])->update(['tkid'=>$ids[0]]);
                    }else{
                        halt($ids);
                        //时间
                    }
                    // return $id;
                }
            }
         



        }
        
    }



    //获取一张照片
    public function get_one_image($id)
    {
        $val = Db::name('door_log')->find($id);
        $url =  'http://106.52.152.213/pictures/'.$val['tkid'].'.jpg';
                
        try {
            $content = file_get_contents($url);
            if(strlen($content)>10){
                
                $img_path = 'pic/'.$val['tkid'].'.jpg';
                // halt($img_path);
                file_put_contents($img_path,$content);
                // halt($this->request->domain().'/'.$img_path);
                
                $res = Db::name('door_log')->where('id',$val['id'])->update(['image'=>'/'.$img_path]);
                if($res)$ci++;
            }
        } catch (\Throwable $th) {
            //throw $th;
        }
    }


    public function send_teacher()
    {
        //获取老师 


        // 循环

         // $Template = new \app\api\controller\Template();
         //    $Template->send_msg($params['idcard'],$temp_url,$status['text']);
    }


}
